#include "Animation.h"

Animation::Animation(){
    currentFrame = 0;
    minFrame = 1;
    maxFrame = 9;   // temporary. may change with new animations
    frameRate = 1000;
    elapsedTime = 0;
    oscillate = false;
    frameInc = 1;
}

//update the current animation frame based on the clock
void Animation::Update(sf::Clock& clock){
    elapsedTime = clock.getElapsedTime().asMilliseconds()- lastTime;
    if (elapsedTime >= frameRate && frameRate != 0){

        //Advance Frame & Time
        currentFrame += frameInc;
        lastTime += elapsedTime;

        //Determine frameIncrement. Oscillate = 0,1,2,max,2,1,0
        if(oscillate) {
            if(frameInc > 0) {
                if(currentFrame >= maxFrame) {
                    frameInc = -frameInc;
                }
            }else{
                if(currentFrame <= 0) {
                    frameInc = -frameInc;
                }
            }
        }else{
            if(currentFrame >= maxFrame) {
                currentFrame = minFrame;
            }
        }

    }
}

//get the current animation frame
int Animation::getCurrentFrame(){
    return currentFrame;
}

//Set the frame rate
void Animation::setFrameRate(int fr){
    frameRate = fr;
}

//Toggle the animation oscillation. Off repeats from 0 to max, on repeats o-max-0
void Animation::toggleOscillate(){
    oscillate = !oscillate;
}

//sets the current frame
void Animation::setCurrentFrame(int frameNumber){
    currentFrame = frameNumber;
}

